package com.aaron.study.kefu.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.aaron.study.common.entity.TextMessage;
import com.aaron.study.kefu.feign.CustomFeignClient;

import lombok.extern.java.Log;

/**
 * 客服人员相关接口 <br>
 * Date: 2018/1/23 <br>
 * Time: 19:23 <br>
 * <p>
 * <a href="https://gitee.com/aaronsd/spring-cloud-security/">spring-cloud-security</a>
 *
 * @author <a href="mailto:610195979@qq.com?subject=com.aaron.study.spring-cloud-security">aaron</a>
 * @version 0.0.1
 */
@Log
@RestController
public class KeFuController {
	@Autowired
	CustomFeignClient		customFeignClient;

	private List<String>	onlinesKeFu;

	@PostConstruct
	public void init() {
		onlinesKeFu = new ArrayList<>();
		onlinesKeFu.add("kefu001");
		onlinesKeFu.add("kefu002");
		onlinesKeFu.add("kefu003");
		onlinesKeFu.add("kefu004");
		onlinesKeFu.add("kefu005");
		onlinesKeFu.add("kefu006");
		onlinesKeFu.add("kefu007");
	}

	@GetMapping("/online/kefu")
	public List<String> onlineKeFu() {
		return onlinesKeFu;
	}

	@PostMapping(value = "/textMessage", produces = "application/json;charset=UTF-8")
	public TextMessage textMessage(@RequestBody TextMessage textMessage) {
		log.info("receive-textMessage " + textMessage);
		textMessage.setToUser(textMessage.getFromUser());
		textMessage.setFromUser(onlinesKeFu.get(RandomUtils.nextInt(onlinesKeFu.size())));
		textMessage.setMessage("亲爱的客户，收到您的消息了我会1尽快处理@" + System.currentTimeMillis());

		return textMessage;
	}

	@PostMapping(value = "/send/customer/text", produces = "application/json;charset=UTF-8")
	public void sendMessage2Customer(@RequestBody TextMessage textMessage) {
		customFeignClient.sendMessage2Customer(textMessage);
	}
}
